package com.yidu.hbatsft.service.impl;

import com.github.pagehelper.PageHelper;
import com.yidu.hbatsft.dao.AtsSchemeAthleteMapper;
import com.yidu.hbatsft.dao.AtsSchemeAuditMapper;
import com.yidu.hbatsft.dao.AtsSchemeExpertMapper;
import com.yidu.hbatsft.dao.AtsSchemeMapper;
import com.yidu.hbatsft.domain.AtsScheme;
import com.yidu.hbatsft.domain.AtsSchemeAthlete;
import com.yidu.hbatsft.domain.AtsSchemeAudit;
import com.yidu.hbatsft.domain.AtsSchemeExpert;
import com.yidu.hbatsft.service.AtsSchemeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Iterator;
import java.util.List;
@Service
public class AtsSchemeServiceImpl implements AtsSchemeService{

    /**
     * 方案表的mapper层
     */
    @Resource
    AtsSchemeMapper atsSchemeMapper;
    /**
     * 方案对应的运动员表的mapper层
     */
    @Resource
    AtsSchemeAthleteMapper atsSchemeAthleteMapper;
    /**
     * 专家咨询表mapper
     */
    @Resource
    AtsSchemeExpertMapper atsSchemeExpertMapper;

    /**
     * 专家审核表mapper
     */
    @Resource
    AtsSchemeAuditMapper atsSchemeAuditMapper;


    /**
     * 运动康复方案专家查询所有
     * @param pageNum
     * @param pageSize
     * @return
     **/
    public List findAllExperkangfu(Integer pageNum,Integer pageSize,AtsScheme atsScheme) {
        //开始的页数和数目
        PageHelper.startPage(pageNum,pageSize);
        List<AtsScheme>sdf= atsSchemeMapper.findAllExperkangfu(atsScheme);
        for (Iterator<AtsScheme> iterator = sdf.iterator(); iterator.hasNext(); ) {
            AtsScheme next =  iterator.next();
            AtsSchemeExpert atsSchemeExpert=atsSchemeExpertMapper.selectByschemeKey(next.getSchemeId());
            if(atsSchemeExpert==null||"".equals(atsSchemeExpert.getAuditState())||atsSchemeExpert.getAuditState()==null){
                next.setAuditState(0);
            }else{
                if(atsSchemeExpert.getAuditState().equals("10")){
                    next.setAuditState(10);
                }else if(atsSchemeExpert.getAuditState().equals("20")){
                   next.setAuditState(20);
                }else if(atsSchemeExpert.getAuditState().equals("21")){
                    next.setAuditState(21);
                }else if(atsSchemeExpert.getAuditState().equals("30")){
                    next.setAuditState(30);
                }else if(atsSchemeExpert.getAuditState().equals("40")){
                    next.setAuditState(40);
                }
            }
        }
        return sdf;
    }

    /**
     *根据id查询
     * @param id
     * @return
     */
    public AtsScheme selectByPrimaryKey(Integer id){
        return atsSchemeMapper.selectByPrimaryKey(id);
    }

    /**
     *查询
     * @param record
     * @return
     */
    @Override
    public List<AtsScheme> selectfindAll(AtsScheme record) {
        List<AtsScheme> ats =  atsSchemeMapper.selectfindAll(record);
        return ats;
    }


    /**
     *逻辑删除
     * @param schemeId
     * @return
     */
    @Override
    public int updatestate(Integer schemeId) {
        int i= atsSchemeMapper.updatestate(schemeId);

        atsSchemeAthleteMapper.updatestate(schemeId);

        AtsSchemeExpert atsSchemeExpert = new AtsSchemeExpert();
        atsSchemeExpert.setSchemeId(schemeId);
        atsSchemeExpert.setIsva(0);
        atsSchemeExpertMapper.updateByschemeKey(atsSchemeExpert);
        atsSchemeAuditMapper.updateAudit(String.valueOf(schemeId));
        return i;
    }

    @Override
    public int ExperkangfuCount(AtsScheme atsScheme) {
        int count= atsSchemeMapper.ExperkangfuCount(atsScheme);
        return  count;
    }

    /**
     * 康复方案查询所有_教练
     * @param pageNum
     * @param pageSize
     * @param atsScheme
     * @return
     */
    @Override
    public List findAllCoachkangfu(Integer pageNum,Integer pageSize,AtsScheme atsScheme) {
        //开始的页数和数目
        PageHelper.startPage(pageNum,pageSize);
        List<AtsScheme>sdf= atsSchemeMapper.findAllCoachkangfu(atsScheme);
        for (Iterator<AtsScheme> iterator = sdf.iterator(); iterator.hasNext(); ) {
            AtsScheme next =  iterator.next();
            AtsSchemeExpert atsSchemeExpert=atsSchemeExpertMapper.selectByschemeKey(next.getSchemeId());
            if(atsSchemeExpert==null||"".equals(atsSchemeExpert.getAuditState())||atsSchemeExpert.getAuditState()==null){
                next.setAuditState(0);
            }else{
                if(atsSchemeExpert.getAuditState().equals("10")){
                    next.setAuditState(10);
                }else if(atsSchemeExpert.getAuditState().equals("20")){
                    next.setAuditState(20);
                }else if(atsSchemeExpert.getAuditState().equals("21")){
                    next.setAuditState(21);
                }else if(atsSchemeExpert.getAuditState().equals("30")){
                    next.setAuditState(30);
                }else if(atsSchemeExpert.getAuditState().equals("40")){
                    next.setAuditState(40);
                }
            }
        }
        return sdf;
    }

    /**
     * 康复方案查询列数_教练
     * @param atsScheme
     * @return
     */
    @Override
    public int CoachkangfuCount(AtsScheme atsScheme) {
        int count=atsSchemeMapper.CoachkangfuCount(atsScheme);
        return count;
    }

    @Override
    public List findAllAthletekangfu(Integer pageNum, Integer pageSize, AtsScheme atsScheme) {
        //开始的页数和数目
        PageHelper.startPage(pageNum,pageSize);
        List<AtsScheme>sdf= atsSchemeMapper.findAllAthletekangfu(atsScheme);
        for (Iterator<AtsScheme> iterator = sdf.iterator(); iterator.hasNext(); ) {
            AtsScheme next =  iterator.next();
            AtsSchemeExpert atsSchemeExpert=atsSchemeExpertMapper.selectByschemeKey(next.getSchemeId());
            if(atsSchemeExpert==null||"".equals(atsSchemeExpert.getAuditState())||atsSchemeExpert.getAuditState()==null){
                next.setAuditState(0);
            }else{
                if(atsSchemeExpert.getAuditState().equals("10")){
                    next.setAuditState(10);
                }else if(atsSchemeExpert.getAuditState().equals("20")){
                    next.setAuditState(20);
                }else if(atsSchemeExpert.getAuditState().equals("21")){
                    next.setAuditState(21);
                }else if(atsSchemeExpert.getAuditState().equals("30")){
                    next.setAuditState(30);
                }else if(atsSchemeExpert.getAuditState().equals("40")){
                    next.setAuditState(40);
                }
            }
        }
        return sdf;
    }

    @Override
    public int AthletekangfuCount(AtsScheme atsScheme) {
        int count =atsSchemeMapper.AthletekangfuCount(atsScheme);
        return count;
    }

    @Override
    public List findAllLeaderkangfu(Integer pageNum, Integer pageSize, AtsScheme atsScheme) {
        //开始的页数和数目
        PageHelper.startPage(pageNum,pageSize);
        List<AtsScheme>sdf= atsSchemeMapper.findAllLeaderkangfu(atsScheme);
        for (Iterator<AtsScheme> iterator = sdf.iterator(); iterator.hasNext(); ) {
            AtsScheme next =  iterator.next();
            AtsSchemeExpert atsSchemeExpert=atsSchemeExpertMapper.selectByschemeKey(next.getSchemeId());
            if(atsSchemeExpert==null||"".equals(atsSchemeExpert.getAuditState())||atsSchemeExpert.getAuditState()==null){
                next.setAuditState(0);
            }else{
                if(atsSchemeExpert.getAuditState().equals("10")){
                    next.setAuditState(10);
                }else if(atsSchemeExpert.getAuditState().equals("20")){
                    next.setAuditState(20);
                }else if(atsSchemeExpert.getAuditState().equals("21")){
                    next.setAuditState(21);
                }else if(atsSchemeExpert.getAuditState().equals("30")){
                    next.setAuditState(30);
                }else if(atsSchemeExpert.getAuditState().equals("40")){
                    next.setAuditState(40);
                }
            }
        }
        return sdf;
    }

    @Override
    public int LeaderkangfuCount(AtsScheme atsScheme) {
        int count=atsSchemeMapper.LeaderkangfuCount(atsScheme);
        return count;
    }

    /**
     *增加的方法
     * @param atsScheme
     * @param atsSchemeAthlete
     * @return
             */
    @Override
    public int add(AtsScheme atsScheme,AtsSchemeAthlete atsSchemeAthlete,String ids) {

        System.out.println("增加执行中");
        int i= atsSchemeMapper.insertSelective(atsScheme);
        System.out.println(atsScheme.getSchemeId()+"方案id");
        System.out.println(atsSchemeAthlete.getAthleteId()+"运动员id");
        atsSchemeAthlete.setSchemeId(atsScheme.getSchemeId());
        atsSchemeAthlete.setIsva(1);
        String[] split = ids.split(",");
        for (int a = 0; a < split.length; a++) {
            String s = split[a];
            atsSchemeAthlete.setAthleteId(Integer.valueOf(s));
            atsSchemeAthleteMapper.insertSelective(atsSchemeAthlete);
        }


        return  i;
    }

    /**
     *修改的方法
     * @param atsSchemeAthlete
     * @param atsScheme
     * @return
     */
    @Override
    public int update(AtsScheme atsScheme,AtsSchemeAthlete atsSchemeAthlete,String ids){
        System.out.println("修改执行中");
        int i= atsSchemeMapper.updateByPrimaryKeySelective(atsScheme);
        System.out.println("开始删除");
        atsSchemeAthleteMapper.deleteBySchemeId(atsScheme.getSchemeId());
        System.out.println(atsScheme.getSchemeId()+"方案id");
        System.out.println(atsSchemeAthlete.getAthleteId()+"运动员id");
        atsSchemeAthlete.setSchemeId(atsScheme.getSchemeId());
        atsSchemeAthlete.setIsva(1);
        if(ids!=null){
            String[] split = ids.split(",");
            for (int a = 0; a < split.length; a++) {
                String s = split[a];
                atsSchemeAthlete.setAthleteId(Integer.valueOf(s));
                atsSchemeAthleteMapper.insertSelective(atsSchemeAthlete);
            }
        }
        return  i;
    }
}

